﻿using UnityEngine;

[RequireComponent(typeof(BoxCollider2D))]
public abstract class UnitEntityBase : USaBehaviour
{
    /// <summary>
    /// 生命周期
    /// </summary>
    public UnitLifeCycle lifeCycle => m_LifeCycle ?? CreateLifeCycle();
    private UnitLifeCycle m_LifeCycle;
    private UnitLifeCycle CreateLifeCycle()
    {
        m_LifeCycle = new UnitLifeCycle();
        m_LifeCycle.Init(this);
        return m_LifeCycle;
    }


    /// <summary>
    /// 物理组件
    /// </summary>
    public UnitPhysics physics => m_Physics ?? CreatePhysics();
    private UnitPhysics m_Physics;
    private UnitPhysics CreatePhysics()
    {
        m_Physics = new UnitPhysics();
        m_Physics.Init(this);
        return m_Physics;
    }


    /// <summary>
    /// 动画机
    /// </summary>
    public UnitAnimator animator => m_Animator ?? CreateAnimator();
    private UnitAnimator m_Animator;
    private UnitAnimator CreateAnimator()
    {
        m_Animator = new UnitAnimator();
        m_Animator.Init(this);
        return m_Animator;
    }


    /// <summary>
    /// 角色控制器
    /// </summary>
    public UnitControllerBase controller => m_Controller ?? CreateController();
    private UnitControllerBase m_Controller;
    protected abstract UnitControllerBase GetController();
    private UnitControllerBase CreateController()
    {
        m_Controller = GetController(); 
        m_Controller.Init(this);
        return m_Controller;
    }


    /// <summary>
    /// 状态机
    /// </summary>
    public FSMBase fsm => m_FSM ?? CreateFSM();
    private FSMBase m_FSM;
    protected abstract FSMBase GetFSM();
    private FSMBase CreateFSM()
    {
        m_FSM = GetFSM();
        m_FSM.Init(this);
        return m_FSM;
    }


    /// <summary>
    /// 属性管理器
    /// </summary>
    public UnitAttributeBase attribute => m_Attribute ?? CreateAttribute();
    private UnitAttributeBase m_Attribute;
    protected abstract UnitAttributeBase GetAttribute();
    private UnitAttributeBase CreateAttribute()
    {
        m_Attribute = GetAttribute();
        m_Attribute.Init(this);
        return m_Attribute;
    }


    /// <summary>
    /// 技能管理器
    /// </summary>
    public SkillManagerBase skillManager => m_SkillManager ?? CreateSkillManager();
    private SkillManagerBase m_SkillManager;
    protected abstract SkillManagerBase GetSkillManager();
    private SkillManagerBase CreateSkillManager()
    {
        m_SkillManager = GetSkillManager();
        m_SkillManager.Init(this);
        return m_SkillManager;
    }


    protected virtual void OnInit() { }
    public void Init()
    {
        OnInit();
    }

    protected virtual void OnSpawn() { }
    public void Spawn()
    {
        lifeCycle.Spawn();
        animator.Spawn();
        attribute.Spawn();
        skillManager.Spawn();
        controller.Spawn();
        physics.Spawn();
        fsm.Spawn();
        OnSpawn();
    }

    protected virtual void OnDespawn() { }
    public void Despawn()
    {
        fsm.Despawn();
        physics.Despawn();
        controller.Despawn();
        skillManager.Despawn();
        attribute.Despawn();
        animator.Despawn();
        lifeCycle.Despawn();
        OnDespawn();
    }

    protected virtual void OnThaw() { }
    public void Thaw()
    {
        lifeCycle.Thaw();
        animator.Thaw();
        attribute.Thaw();
        skillManager.Thaw();
        controller.Thaw();
        physics.Thaw();
        fsm.Thaw();
        OnThaw();
    }

    protected virtual void OnFreeze() { }
    public void Freeze()
    {
        OnFreeze();
        fsm.Freeze();
        physics.Freeze();
        controller.Freeze();
        skillManager.Freeze();
        attribute.Freeze();
        animator.Freeze();
        lifeCycle.Freeze();
    }
}